package com.oms.service.impl;

import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.oms.dao.AcknowledgementDao;
import com.oms.dao.BaseDao;
import com.oms.dao.MessageDao;
import com.oms.domain.Acknowledgement;
import com.oms.domain.Message;
import com.oms.domain.User;
import com.oms.service.MessageService;

@Transactional
@Service(value="messageService")
public class MssageServiceImpl extends BaseServiceImpl<Message> implements
		MessageService {
	
	@Resource(name = "messageDao")
	MessageDao messageDao;
	
	@Resource(name="acknowledgementDao")
	AcknowledgementDao acknowledgementDao;
	
   @Resource(name = "messageDao")  
    public void setDao(BaseDao<Message> dao) {  
        super.setDao(dao);  
    }



	@Override
	public void sendMessage(User sender, User userByReceiver,
			Short messageType, String messageTitle, String messageComment) {
		
		Timestamp sendTimestamp = new Timestamp(System.currentTimeMillis()); 	//发送消息的时间
		Message	message=new Message(sender, messageType, messageTitle, messageComment, sendTimestamp,
				false, false);	//创建信息条目
		Integer messageId=(Integer) messageDao.save(message);	//存放消息到数据库，并得到信息的自增主键
		message.setMessageId(messageId);	//给原信息设置主键
		//新建收到信件条目
		Acknowledgement acknowledgement=new Acknowledgement(userByReceiver, message, false,sendTimestamp, false);
		//存放到数据库(完成发送)
		acknowledgementDao.save(acknowledgement);
	}

	@Override
	public void sendMessage(User sender, List<User> receiverList,
			Short messageType, String messageTitle, String messageComment) {
		
		Timestamp sendTimestamp = new Timestamp(System.currentTimeMillis()); 	//发送消息的时间
		Message	message=new Message(sender, messageType, messageTitle, messageComment, sendTimestamp,
				false, false);	//创建信息条目
		Integer messageId=(Integer) messageDao.save(message);	//存放消息到数据库，并得到信息的自增主键
		message.setMessageId(messageId);	//给原信息设置主键
		
		//遍历发送消息
		for(User userByReceiver:receiverList){
			Acknowledgement acknowledgement=new Acknowledgement(userByReceiver, message, false,sendTimestamp, false);
			acknowledgementDao.save(acknowledgement);
		}
	}

	@Override
	public void deleteMsg(Integer id, User user) {
		Message message=messageDao.findById(id);
		//判断查询结果是否为空
		if(message==null || message.getDeleteTag()==true){
			throw new RuntimeException("0");	//抛出找不到异常
		}
		//判断传入用户是否有权限删除
		if(user==null || !message.getUser().getUserId().equals(user.getUserId())){
			throw new RuntimeException("1");	//抛出无权限异常
		}
		message.setDeleteTag(true);
		messageDao.update(message);;
	}

	@Override
	public Message senderReadMsg(Integer id, User user) {
		Message message=messageDao.findById(id);
		//判断查询结果是否有效
		if(message==null || message.getDeleteTag()){
			return null;	//返回值无效，返回空
		}
//		System.out.println(message.getUser().getUserId());
//		System.out.println(user.getUserId());
		//是否有阅读权限
		if(user==null ||!user.getUserId().equals(message.getUser().getUserId())){
			throw new RuntimeException("1");	//抛出权限不足异常
		}
		return message;
	}



	@Fetch(FetchMode.JOIN)
	@Override
	public List<Message> getMessageList(User sender, Timestamp startTime,
			Timestamp endTime) {
		//如果没有选时间段，则显示将时间段限制为不受限制
		if(startTime==null){
			startTime=new Timestamp(0);
		}
		if(endTime==null){
			endTime=new Timestamp(System.currentTimeMillis()+1000*3600);
		}
		String hql = "from Message message where message.user=?0 and message.sendTime between ?1 and ?2 and message.deleteTag=0";
		return messageDao.list(hql, sender,startTime,endTime);
	}


	@Fetch(FetchMode.JOIN)
	@Override
	public List<Message> getMessageListWithPage(User sender, Integer firstResult,
			Integer maxResults, Timestamp startTime, Timestamp endTime) {
		//如果没有选时间段，则显示将时间段限制为不受限制
		if(startTime==null){
			startTime=new Timestamp(0);
		}
		if(endTime==null){
			endTime=new Timestamp(System.currentTimeMillis()+1000*3600);
		}
		
		String hql = "from Message message where message.user=?0 and message.sendTime between ?1 and ?2 and message.deleteTag=0";
		return messageDao.listWithPage(hql, firstResult,maxResults,sender,startTime,endTime);
	}

}
